package com.dili.dd.gpi.controller;

import com.dili.dd.gpi.domain.EventParam;
import com.dili.dd.gpi.enums.ParamType;
import com.dili.dd.gpi.service.DmaEventParamService;
import com.dili.dd.gpi.util.spring.JsonView;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * Created by 思思君 on 2015/10/20.
 */
@Controller
@RequestMapping("/dma/event/param")
public class DmaEventParamController
{
    @Resource
    private DmaEventParamService dmaEventParamService;

    @RequestMapping(value = "/html/list", method = RequestMethod.GET)
    public String toList(String eventId, Model model)
    {
        List<EventParam> applicationList = this.dmaEventParamService.getEventParamsByEevntId(eventId);
        model.addAttribute("paramList", applicationList);
        model.addAttribute("eventId", eventId);
        return "/WEB-INF/views/dma/event/param_list";
    }

    @RequestMapping(value = "/html/create", method = RequestMethod.GET)
    public String toCreate(Model model, String eventId)
    {
        model.addAttribute("paramTypeList", ParamType.values());
        model.addAttribute("eventId", eventId);
        return "/WEB-INF/views/dma/event/param_add";
    }

    @RequestMapping(value = "/html/update", method = RequestMethod.GET)
    public String toUpdate(Integer id, String eventId, Model model)
    {
        EventParam eventParam = dmaEventParamService.getEventParamById(id);
        model.addAttribute("param", eventParam);
        model.addAttribute("paramTypeList", ParamType.values());
        model.addAttribute("eventId", eventId);
        return "/WEB-INF/views/dma/event/param_edit";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ModelAndView createParam(EventParam eventParam) throws ParseException
    {
        List<EventParam> eventParamList = dmaEventParamService.getEventParamByName(eventParam.getParamName(), eventParam.getEventId());
        if (eventParamList != null && !eventParamList.isEmpty())
        {
            return new ModelAndView(new JsonView(false, "参数名称已经存在，无法创建应用！"));
        }

        eventParam.setCreateDate(new Date());
        eventParam.setUpdateDate(new Date());
        if (dmaEventParamService.createEevntParam(eventParam) == 1)
        {
            return new ModelAndView(new JsonView(true,"参数添加成功！"));
        }
        else
        {
            return new ModelAndView(new JsonView(false, "参数添加失败！"));
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ModelAndView update(EventParam eventParam)
    {
        eventParam.setUpdateDate(new Date());
        if (dmaEventParamService.updateEventParam(eventParam) == 1)
        {
            return new ModelAndView(new JsonView(true, "修改成功"));
        }

        return new ModelAndView(new JsonView(false, "修改失败"));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ModelAndView delete(Integer id)
    {
        if (dmaEventParamService.deleteEventParamById(id) == 1)
        {
            return new ModelAndView(new JsonView(true, "删除成功"));
        }

        return new ModelAndView(new JsonView(false, "删除失败"));
    }
}
